package com.zxkj.commonpatrol.viewmodel;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.czy.basecommon.utils.base.BaseRepository;
import com.czy.basecommon.utils.base.BaseViewModel;
import com.czy.basecommon.utils.base.StateData;
import com.czy.basecommon.utils.base.StateLiveData;
import com.zxkj.commonpatrol.model.post.PointNewDevice;
import com.zxkj.commonpatrol.model.response.DevTypeExtendInfo;
import com.zxkj.commonpatrol.model.response.DeviceType;
import com.zxkj.commonpatrol.model.response.PointInspectDeviceResponse;
import com.zxkj.commonpatrol.repoisity.AddCheckPointRepoisity;
import com.zxkj.commonpatrol.repoisity.AddDeciveRepoisity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AddDeciveViewModel extends BaseViewModel<AddDeciveRepoisity> {

public StateLiveData<List<String>> datas=new StateLiveData<>();
    public StateLiveData<PointInspectDeviceResponse> ShowDatas=new StateLiveData<>();
    public ObservableField<String> PointNo = new ObservableField<>("");
    public ObservableField<String> RSName = new ObservableField<>("");

    public ObservableField<String> PointType = new ObservableField<>("");

    public ObservableField<String> EName = new ObservableField<>("");

    public ObservableField<String> CName = new ObservableField<>("");

    public ObservableField<String> Loc = new ObservableField<>("");

    public ObservableField<String> Time = new ObservableField<>("");
    public ObservableField<String> CPNB = new ObservableField<>("");
    public ObservableField<String> ZS = new ObservableField<>("");
    public ObservableField<String> ZL = new ObservableField<>("");
    public ObservableField<String> QuestList = new ObservableField<>("");

     public StateLiveData<List<DeviceType>> TypeListDatas=new StateLiveData<>();
     public StateLiveData< List<DevTypeExtendInfo.DeviceProperty> >DevicePropertys=new StateLiveData<>();
    public StateLiveData< List<DevTypeExtendInfo.Questions> >DeviceQuests=new StateLiveData<>();
    public StateLiveData<Object> save=new StateLiveData();
    public StateLiveData<String> QuestsNo=new StateLiveData();
    public StateLiveData<String> DevTypeCode=new StateLiveData();
    public StateLiveData<List<String>>Codes=new StateLiveData<>();
    public StateLiveData<List<String>>ZLListData=new StateLiveData<>();
    public AddDeciveViewModel(@NonNull Application application) {
        super(application);
    }


       public void ShowDetail( String RouteInspectNo,String PointNos,String DeviceNo){
           mRepository.ShowDetail(RouteInspectNo, PointNos, DeviceNo, new BaseRepository.MyBaseDataCallBack() {
               @Override
               public void onDataSuccessCallBack(Object o) {
                   PointInspectDeviceResponse response =(PointInspectDeviceResponse)o;
                   if (response!=null){
                       ShowDatas.postSuccess(response);
                       List<String> strings=new ArrayList<>();

                       PointNo.set(response.DeviceCode);
                       RSName.set(response.RSName);
//                       PointType.set(response.);
                       EName.set(response.DeviceEName);
                       CName.set(response.DeviceCName);
                       PointType.set(response.DevTypeName);
                       DevTypeCode.postSuccess(response.getDevTypeCode());
                       QuestsNo.postSuccess(response.getQuestsNo());
                       Loc.set(response.DevLocation);
                       QuestList.set(response.QuestsName);

                       if (response.getQuesItems()!=null){
                           if (response.getQuesItems().size()>0){
                               List<String> da=new ArrayList<>();
                               for (int i = 0; i < response.getQuesItems().size(); i++) {
                                   da.add(response.getQuesItems().get(i).Name);
                               }
                               datas.postSuccess(da);
                           }

                       }


                   }

               }

               @Override
               public void onDataFailCallBack(Object o, Object t1) {
                   ShowDatas.postFailure(404,(String)t1);
               }
           });
       }
//    public void  getQuestList(){
//        mRepository.getQuestList(new BaseRepository.MyBaseDataCallBack<List<String>>() {
//            @Override
//            public void onDataSuccessCallBack(List<String> strings) {
//                datas.postSuccess(strings);
//            }
//
//            @Override
//            public void onDataFailCallBack(List<String> strings, List<String> t1) {
//
//            }
//        });
//    }

    public  void  getDeviceType(String PointCode){
        mRepository.getDeviceType(PointCode, new BaseRepository.MyBaseDataCallBack() {
            @Override
            public void onDataSuccessCallBack(Object o) {
                List<DeviceType> DeviceTypes=(List<DeviceType>)o;
                if (DeviceTypes==null){
                    DeviceTypes=new ArrayList<>();
                }
                TypeListDatas.postSuccess(DeviceTypes);
            }

            @Override
            public void onDataFailCallBack(Object o, Object t1) {
                TypeListDatas.postFailure(404,(String)t1);
            }
        });
    }


    public void GetDevTypeExtendInfo(String RouteInspectNo,String DevTypeNo){
        mRepository.GetDevTypeExtendInfo(RouteInspectNo, DevTypeNo, new BaseRepository.MyBaseDataCallBack() {
            @Override
            public void onDataSuccessCallBack(Object o) {
                DevTypeExtendInfo bean=(DevTypeExtendInfo)o;
                List<DevTypeExtendInfo.DeviceProperty> devPropertys = bean.getDevPropertys();
                if (devPropertys==null){
                    devPropertys=new ArrayList<>();
                }
//                if (devPropertys.size()>0){
//                    ZS.set(bean.getDevPropertys().get(0).Value);
//                    CPNB.set(bean.getDevPropertys().get(1).Value);
//                    Time.set(bean.getDevPropertys().get(2).Value);
//                    ZL.set(bean.getDevPropertys().get(3).Value);
//                }
                DevicePropertys.postSuccess(devPropertys);
                List<DevTypeExtendInfo.Questions> quests = bean.getQuests();
                if (quests==null){
                    quests=new ArrayList<>();
                }
                  DeviceQuests.postSuccess(quests);

            }

            @Override
            public void onDataFailCallBack(Object o, Object t1) {
                DevicePropertys.postFailure(404,(String)t1);
            }
        });
    }

    public  void Save(String DevTypeCode, String QuestsNo, Map<String, String> values, String Point, String Rou, int isAdd){
          PointNewDevice  device1 =new PointNewDevice();
          device1.setDeviceCode(PointNo.get());
          device1.setDeviceCName(CName.get());
          device1.setDeviceEName(EName.get());
          device1.setDevLocation(Loc.get());
          device1.setDevTypeCode(DevTypeCode);
          device1.setQuestsNo(QuestsNo);
          device1.setIsAdd(isAdd+"");
          device1.setPointNo(Point);
          device1.setRouteInspectNo(Rou);


        List< PointNewDevice.DevPropertyValue> devPropertyValues=new ArrayList<>();
        if (values!=null){
            for(Map.Entry<String, String> entry : values.entrySet()){
                String mapKey = entry.getKey();
                String mapValue = entry.getValue();
                PointNewDevice.DevPropertyValue devPropertyValue =new PointNewDevice.DevPropertyValue();
                devPropertyValue.setDeviceNo(PointNo.get());
                devPropertyValue.setPropertyCode(mapKey);
                devPropertyValue.setPropertyValue(mapValue);
                devPropertyValues.add(devPropertyValue);
            }
        }

        device1.setDevPropertyValues(devPropertyValues);



        mRepository.Save(device1, new BaseRepository.MyBaseDataCallBack() {
            @Override
            public void onDataSuccessCallBack(Object o) {
                save.postSuccess(o);
            }

            @Override
            public void onDataFailCallBack(Object o, Object t1) {
                save.postFailure(404,(String)t1);
            }
        });


    }

}
